package com.hainatong.shiphelper.module.handlejob.fragment;

import android.os.Bundle;
import android.view.View;
import com.base.common.utils.UIUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.GlobalConfig;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentHandoverJobBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.handlejob.VoyageActivity;
import com.hainatong.shiphelper.module.handlejob.adapter.BossTagAdapter;
import com.hainatong.shiphelper.module.handlejob.entity.HandleParam;
import com.hainatong.shiphelper.module.handlejob.entity.HandleTypeBean;
import com.hainatong.shiphelper.module.handlejob.entity.VoyageBean;
import com.hainatong.shiphelper.module.handlejob.viewmodel.HandleJobViewModel;
import com.hainatong.shiphelper.module.holiday.PortActivity;
import com.hainatong.shiphelper.module.holiday.entity.PortBean;
import com.hainatong.shiphelper.module.workermgr.WorkerMgrActivity;
import com.hainatong.shiphelper.module.workermgr.entity.WorkerBean;
import com.hainatong.shiphelper.module.workermgr.fragment.WorkerFragment;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.jeremyliao.liveeventbus.LiveEventBus;


import java.util.ArrayList;
import java.util.List;


public class HandleWorkFragment extends MvvmLazyFragment<FragmentHandoverJobBinding, HandleJobViewModel>
{
    private static String HANDLE_TAG = "HandleWorkFragment";

    public static int  handleWorkType = 1;
    public static int  onShipType = 2;
    public static int  fireWorkType = 3;
    public static int  jiabanWorkType = 4;

    private int handleType;
    private HandleTypeBean handleTypeBean;

    private BossTagAdapter<WorkerBean> tagAdapter;

    private List<WorkerBean> workerList;

    private HandleParam handleParam;

    public static HandleWorkFragment newInstance(String typeCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( HANDLE_TAG , typeCode );
        HandleWorkFragment handleWorkFragment = new HandleWorkFragment();
        handleWorkFragment.setArguments( bundle );
        return handleWorkFragment;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_handover_job;
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        String type = "";
        if(  args != null )
        {
            type = args.getString(HANDLE_TAG , "");
        }

        if( !StringUtils.isEmpty( type ))
        {
            handleTypeBean = GsonUtil.json2Obj(type,HandleTypeBean.class);
        }
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
        handleParam = new HandleParam();
        workerList = new ArrayList<>();
        // 航次
        viewDataBinding.rlVoyageLayout.setOnClickListener( v ->
        {
            //ToastUtils.showShort( "选择航次" );
            VoyageActivity.start(getActivity());
        });

        // 选择港口
        viewDataBinding.rlThePortLayout.setOnClickListener( v -> {

            PortActivity.start( getActivity() );
        } );
        // 选择发送人
        viewDataBinding.btnImgAdd.setOnClickListener( v -> {
            // 通知 开始 选 用户
            LiveEventBus.get(WorkerFragment.SELECT_WORKER_TAG ,Boolean.class ).post( true );
            WorkerMgrActivity.start( getActivity() );
        } );


        // 下一步 签名
        viewDataBinding.btnNext.setOnClickListener( v -> {



            ComFragmentActivity.startViewByParam( getActivity(),Constants.VIEW_SIGN_VIEW , GsonUtil.obj2json( handleParam ) );

            // 提交 交接 申请
            //viewModel.submitHandleJobTask(  );

        } );

        // 拿到 选择的港口
        LiveEventBus.get(PortActivity.PORT_RESULT , PortBean.class)
                .observeSticky( this , portBean -> {
                    // 获取到 港口 信息
                    viewDataBinding.tvPortName.setText( portBean.getName() );
                    //设置 港口
                    // 设置港口
                    handleParam.setPort( portBean.getName() );
                    handleParam.setPort_id( portBean.getUuid() );
                    // "Abo" "b26a781d-863b-11eb-b45c-5254006b78b4"
                } );

        // 拿到选择的 用户
        LiveEventBus.get(WorkerFragment.SELECT_WORKER_TAG ,WorkerBean.class )
                .observeSticky( this , theWorker -> {
                    setHandleWorkerInfo(theWorker);
                } );

        //拿到航次
        LiveEventBus.get(VoyageActivity.VOYAGE_RESULT, VoyageBean.class)
                    .observeSticky( this,theVoyage ->{
                        // 设置 航次
                        viewDataBinding.tvTheVoyage.setText( theVoyage.getRouteName() );
                        handleParam.setVoyage( theVoyage.getRouteName() );
                        handleParam.setVoyage_id(theVoyage.getUuid());
                        //handleParam.setVoyage( "SGA-GOOD HOP" );
                        //handleParam.setVoyage_id("c158e99d-0cb0-4e3d-bb7e-c99091ccf49f");
                    } );
    }


    @Override
    protected void lazyLoad()
    {
        if(UIUtils.isNotEmpty( GlobalConfig.getInstance().getUserInfo() ) )
        {

            viewDataBinding.tvShipName.setText( GlobalConfig.getInstance().getUserInfo().getShip_name() );
            // 设置 基本信息
            handleParam.setCompany( GlobalConfig.getInstance().getUserInfo().getCompany()  );
            handleParam.setCompany_id( GlobalConfig.getInstance().getUserInfo().getCompany_id() );
            handleParam.setShip_id( GlobalConfig.getInstance().getUserInfo().getShip_id());
            handleParam.setShipname( GlobalConfig.getInstance().getUserInfo().getShip_name() );

            handleParam.setCreator( GlobalConfig.getInstance().getUserInfo().getUserTrueName() );
            handleParam.setCreateID( GlobalConfig.getInstance().getUserInfo().getUuid() );
        }

        if(ObjectUtils.isNotEmpty( handleTypeBean ))
        {
            //设置 交接类型
            handleParam.setHandover_type( handleTypeBean.getDutyType() );
            handleParam.setHandover_type_id( handleTypeBean.getDutyTypeId() );
        }



        /*
        WorkerBean boss = new WorkerBean();
        boss.setUserTrueName("Boss001");
        workerList.add(boss);
        tagAdapter = new BossTagAdapter<WorkerBean>(workerList );
        */
    }


    private void setHandleWorkerInfo(WorkerBean worker)
    {
        handleParam.setSuccessor( worker.getUserTrueName() );
        handleParam.setSuccessor_id( worker.getUuid() );
        workerList.clear();
        workerList.add( worker );
        tagAdapter = new BossTagAdapter<WorkerBean>(workerList );
        tagAdapter.setClearClickListener((position, boss) -> {
            workerList.remove(position);
            tagAdapter = null;
            tagAdapter = new BossTagAdapter<WorkerBean>(workerList);
            viewDataBinding.tflBossTags.setAdapter( tagAdapter );
        });
        viewDataBinding.tflBossTags.setAdapter( tagAdapter );
    }

}
